#include "player.h"
#include<QPixmap>
#include"GameSetting.h"
#include<QKeyEvent>
#include"enemy.h"
#include"bullet.h"
#include<QGraphicsScene>
#include"score.h"
#include"health.h"
#include<QGraphicsTextItem>
#include<QFontMetrics>

using namespace GameSetting;

Player::Player(QGraphicsItem *parent):QGraphicsPixmapItem(parent)
{
    bulletSound.setSource(QUrl(""));
    setPixmap(QPixmap(":/images/Player-removebg-preview.png"));
    setScale(PlayerScale);
    setPos(SceneWidth/2-boundingRect().width()*PlayerScale/2,
           SceneHeight-boundingRect().height()*PlayerScale );
    setFlag(QGraphicsItem::ItemIsFocusable);
    setFocus();
    startTimer(1000);


}


void Player::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
        case Qt::Key_A:
            if (pos().x() > 0)
                setPos(x() - PlayerMoveSpeed, y());  // 左移：X 减少
            break;

        case Qt::Key_D:
            if (pos().x() < SceneWidth - boundingRect().width() * PlayerScale)
                setPos(x() + PlayerMoveSpeed, y());  // 右移：X 增加
            break;

        case Qt::Key_W:
            if (pos().y() > boundingRect().height() / 5)  // 假设限制最小 Y 值
                setPos(x(), y() - PlayerMoveSpeed);  // 上移：Y 减少
            break;

        case Qt::Key_S:
            if (pos().y() < SceneHeight - boundingRect().height()* PlayerScale)  // 限制最大 Y 值
                setPos(x(), y() + PlayerMoveSpeed);  // 下移：Y 增加
            break;

        case Qt::Key_Return:
            if(playing)
            {
                return;
            }

            playing = true;
            Health::getInstance().reset();
            Score::getInstance().reset();
            messageItem->hide();

        case Qt::Key_Space:
            bulletSound.play();
            Bullet* bullet = new Bullet;
            int temp = x()+ boundingRect().width() * PlayerScale/2;//只考虑player宽度
            temp -= bullet-> boundingRect().width() * BulletScale/2;//考虑bullet，向左移动半个宽度
            bullet->setPos(temp,y());
            scene()->addItem(bullet);
            break;
    }



}

void Player::enemySpawn()
{

        Enemy* enemy = new Enemy;
        scene()->addItem(enemy);


}

void Player::gameOver()
{
    playing = false;
    for(auto item:scene()->items())
    {
        if(typeid(*item)==typeid(Enemy))
        {
            scene()->removeItem(item);
            delete item;
        }
    }
    if(!messageItem)
    {
        messageItem = new QGraphicsTextItem;
        scene()->addItem(messageItem);
        QString message("Game Over!按回车键重新开始！");
        messageItem->setPlainText(message);
        messageItem->setDefaultTextColor(Qt::black);
        QFont font(QFont("Courier New",GameSetting::FontSize*2,QFont::Bold));
        messageItem->setFont(font);
        QFontMetrics fm(font);
        int msgWidth = fm.horizontalAdvance(message);
        messageItem->setPos(GameSetting::SceneWidth/2-msgWidth/2,
                            GameSetting::SceneHeight/2);
    }
    else
    {
        messageItem->show();
    }
}

void Player::timerEvent(QTimerEvent *)
{
    if(playing)
    {
        enemySpawn();
    }
    if(Health::getInstance().getHealth()<=0)
    {
        gameOver();
    }
}
